package user_service

import (
	"errors"
	"leeframe/internal/model"
	"leeframe/internal/pkg/logger"
	"leeframe/internal/pkg/mysql"
	"leeframe/internal/schema"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// List
// @Author: lee
// @Date: 2021-11-29 16:42:48
// @Description: 获取用户列表
func List(ctx *gin.Context, req *schema.UserListReq) (paginationResp *schema.PaginationResp, err error) {
	user := new(model.User)
	db := mysql.GetDB()
	list, err := user.List(ctx, db, req.Pagination)
	if err != nil {
		logger.ErrorN(ctx, err)
		return nil, err
	}

	count, err := user.ListCount(ctx, db)
	if err != nil {
		logger.ErrorN(ctx, err)
		return
	}

	listData := make([]*schema.UserListData, len(list))
	for i, v := range list {
		listData[i] = &schema.UserListData{
			ID:       v.ID,
			Username: v.Username,
		}
	}

	return &schema.PaginationResp{
		Total: count,
		List:  listData,
	}, nil
}

// Info
// @Author: lee
// @Date: 2021-11-30 11:29:00
// @Description: 根据ID获取用户信息
func Info(ctx *gin.Context, id int) (info *schema.UserInfoData, err error) {
	user := new(model.User)
	db := mysql.GetDB()
	var params []interface{}
	where := ""

	where += "id = ?"
	params = append(params, id)

	res, err := user.Info(ctx, db, where, params)
	if err != nil {
		logger.ErrorN(ctx, "get user info err: ", err)
		return nil, err
	}
	info = &schema.UserInfoData{
		ID:        res.ID,
		Username:  res.Username,
		CreatedAt: res.CreatedAt.UnixMilli(),
	}

	return
}

// Create
// @Author: lee
// @Date: 2021-11-30 11:29:00
// @Description: 创建用户
func Create(ctx *gin.Context, username string) (id int, err error) {
	// 检查用户名是否重复
	isExist, err := CheckUsernameIsExist(ctx, username)
	if err != nil {
		logger.ErrorN(ctx, "check username is exist err: ", err)
		return 0, err
	}
	// 用户名已存在
	if isExist {
		return 0, errors.New("username is already exist")

	}

	user := new(model.User)
	db := mysql.GetDB()
	res, err := user.Create(ctx, db, username)
	if err != nil {
		logger.ErrorN(ctx, "create user err: ", err)
		return 0, err
	}
	id = res

	return
}

// CheckUsernameIsExist
// @Author: lee
// @Date: 2021-12-06 18:09:00
// @Description: 检查用户名是否重复
func CheckUsernameIsExist(ctx *gin.Context, username string) (isExist bool, err error) {
	user := new(model.User)
	db := mysql.GetDB()

	var params []interface{}
	where := ""

	where += "username = ?"
	params = append(params, username)

	_, err = user.Info(ctx, db, where, params)
	// 找不到记录，gorm会报错
	if errors.Is(err, gorm.ErrRecordNotFound) {
		return false, nil
	}
	if err != nil {
		logger.ErrorN(ctx, "check username is exist err: ", err)
		return false, err
	}

	return true, nil
}

// Delete
// @Author: lee
// @Date: 2021-12-23 17:25:05
// @Description: 根据ID删除用户
func Delete(ctx *gin.Context, id int) (err error) {
	user := new(model.User)
	db := mysql.GetDB()
	var params []interface{}
	where := ""

	where += "id = ?"
	params = append(params, id)

	if err := user.Delete(ctx, db, where, params); err != nil {
		logger.ErrorN(ctx, "delete user err: ", err)
		return err
	}
	return
}
